package com.neusmart.tongrentang.dialog;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.GridView;

import com.neusmart.common.dialog.BaseDialog;
import com.neusmart.tongrentang.R;
import com.neusmart.tongrentang.adapter.EmojiAdapter;
import com.neusmart.tongrentang.adapter.EmojiPageAdapter;
import com.neusmart.tongrentang.model.Emoji;
import com.neusmart.tongrentang.util.EmojiJsonTool;
import com.neusmart.tongrentang.view.EmojiViewPager;
import com.viewpagerindicator.CirclePageIndicator;

import java.util.ArrayList;
import java.util.List;

/**
 * Reply Dialog
 * <p/>
 * roy
 */
public class ReplyDialog extends BaseDialog implements AdapterView.OnItemClickListener {

    private static final int LINE_EMOJI_COUNT = 7;

    private EmojiViewPager viewPager;
    private CirclePageIndicator indicator;

    private EmojiPageAdapter adapter;
    private List<Emoji> emojiList;
    private List<View> gridViewList = new ArrayList<>();
    private int next = 0;

    public ReplyDialog(Context context) {
        super(context, R.style.DialogTheme);
        init();
    }

    @Override
    protected int getLayoutResID() {
        return R.layout.reply_dialog;
    }

    @Override
    protected void initView() {
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        mainView = findViewById(R.id.main);
        viewPager = (EmojiViewPager) findViewById(R.id.emoji_viewpager);
        indicator = (CirclePageIndicator) findViewById(R.id.emoji_indicator);
        getView();
        viewPager.setAdapter(adapter);
        indicator.setViewPager(viewPager);
    }

    @Override
    protected void setListener() {
        super.setListener();
    }

    private void getView() {
        emojiList = EmojiJsonTool.parseEmojiData(mContext);
        Emoji delete = emojiList.get(emojiList.size() - 1);

        for (int j = 1; j < emojiList.size(); j++) {
            if (j % (LINE_EMOJI_COUNT * 3) == 0) {
                emojiList.add(j - 1, delete);
            }
        }
        getGridView();
    }

    private void getGridView() {
        boolean bool = true;
        while (bool) {
            GridView gridView = new GridView(mContext);
            int result = next + LINE_EMOJI_COUNT * 3;
            if (emojiList.size() != 0 && result < emojiList.size()) {
                gridView.setNumColumns(LINE_EMOJI_COUNT);
                EmojiAdapter emojiAdapter = new EmojiAdapter(mContext, emojiList.subList(next, result));
                gridView.setAdapter(emojiAdapter);
                next = result;
                gridViewList.add(gridView);
            } else if (result - emojiList.size() <= LINE_EMOJI_COUNT * 3) {
                gridView.setNumColumns(LINE_EMOJI_COUNT);
                EmojiAdapter emojiAdapter = new EmojiAdapter(mContext, emojiList.subList(next, emojiList.size()));
                gridView.setAdapter(emojiAdapter);
                next = emojiList.size() - 1;
                gridViewList.add(gridView);
                bool = false;
            } else {
                bool = false;
            }
            gridView.setOnItemClickListener(this);
            gridView.setBackgroundColor(Color.TRANSPARENT);
            gridView.setHorizontalSpacing(1);
            gridView.setVerticalSpacing(40);
            gridView.setStretchMode(GridView.STRETCH_COLUMN_WIDTH);
            gridView.setCacheColorHint(0);
            gridView.setPadding(35, 35, 35, 35);
            gridView.setSelector(new ColorDrawable(Color.TRANSPARENT));
            gridView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT));
            gridView.setGravity(Gravity.CENTER);
        }
        adapter = new EmojiPageAdapter(gridViewList);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    }
}
